package com.example.nyf.web.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.nyf.common.AjaxResult;
import com.example.nyf.dto.SysMenuDTO;
import com.example.nyf.dto.SysRoleDTO;
import com.example.nyf.entity.SysMenu;
import com.example.nyf.entity.SysRole;
import com.example.nyf.service.SysMenuService;
import com.example.nyf.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class RoleController extends BaseController {
    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    SysMenuService sysMenuService;

    /**
     * 角色列表页面
     * @return
     */
    @RequestMapping("/system/role_list")
    public ModelAndView roleList() {
        ModelAndView modelAndView = new ModelAndView("system/role_list");

        return modelAndView;
    }

    @RequestMapping("/system/get_role_list")
    @ResponseBody
    public String roleListAjax() {
        String data = "";

        List<SysRole> list = sysRoleService.getSysRoleList();
        if(list!=null && !list.isEmpty()) {
            data = JSON.toJSONString(list);
        }
        return data;
    }

    @RequestMapping("/system/add_role")
    public ModelAndView roleInfo(@RequestParam(value = "id", required = false, defaultValue = "0") Integer id) {
        String menu_list = "{}";
        String role = "{menu_id_array:[]}"; // 前端展示需要

        SysRoleDTO sysRoleDTO = sysRoleService.getRoleMenuById(id);
        if(sysRoleDTO != null) {
            if(sysRoleDTO.getMenu_id()!=null) {
                String[] str = {};
                str = sysRoleDTO.getMenu_id().split(",");
                // 前端数组内数字需数字类型才能比较
                Integer[] num= new Integer[str.length];
                for(int i=0; i<num.length; i++) {
                    num[i] = Integer.parseInt(str[i]);
                }

                sysRoleDTO.setMenu_id_array(num);
            } else {
                Integer[] num= {};
                sysRoleDTO.setMenu_id_array(num);
            }
            role = JSON.toJSONString(sysRoleDTO);
        }

        List<SysMenuDTO> list = sysMenuService.listSysMenuAll();
        if(list!=null && !list.isEmpty()) {
            menu_list = JSON.toJSONString(list);
        }

        ModelAndView modelAndView = new ModelAndView("system/add_role");
        modelAndView.addObject("menu_list", menu_list);
        modelAndView.addObject("role", role);

        return modelAndView;
    }

    @PostMapping("/system/save_role")
    @ResponseBody
    public AjaxResult saveSysRole(@RequestBody SysRoleDTO sysRoleDTO) {
        //JSON字符串转换成Java对象
        //SysRoleDTO sysRoleDTO = JSONObject.parseObject(post, SysRoleDTO.class);
        if(sysRoleDTO.getId()==null || "".equals(sysRoleDTO.getId())) {

            sysRoleDTO.setAdd_user_id(getSysUser().getId());
            sysRoleDTO.setAdd_user_name(getSysUser().getUser_name());
            sysRoleDTO.setAdd_time((int)(System.currentTimeMillis()/1000));
            sysRoleService.insertSysRole(sysRoleDTO);
        } else {
            sysRoleService.updateSysRole(sysRoleDTO);
        }

        return AjaxResult.success();
    }

    @RequestMapping("/system/delete_role")
    @ResponseBody
    public AjaxResult deleteRole(@RequestParam("id") Integer id) {
        sysRoleService.deleteSysRole(id);

        return AjaxResult.success();
    }
}
